package com.mm.scheme.domian.po;


import com.mm.scheme.config.AiUtils;
import com.mm.scheme.exception.AiException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 * @BelongsProject: chatgpt-java-develop
 * @BelongsPackage: com.unfbx.common
 * @Author : 游在海里的鱼
 * @CreateTime : 2024-03-22  21:25
 * @Version : 1.0
 * @ClassName : TODO
 * @Description : TODO
 */
@Data
@Slf4j
public class Step {
    //参数map
    private Map<String, Object> paramMap;
    //返回
    private String ret;
    //方法
    private int func;
    //输入
    private String input;

    private AiUtils aiUtils;

    public String function() throws AiException {
            switch (func){
                //1.替换
                case 1:
                    String[] params1 = input.split(",|，",2);
                    if(params1.length != 2){
                        return null;
                    }
                    String beRep = new String((String) paramMap.get(params1[0]));
                    String repIn = (String) paramMap.get(params1[1]);

                    if(ret == null) return null;
                    paramMap.put(ret,beRep.replaceFirst("\\[&]",repIn));
                    return ret;
                //2.分割
                case 2:
                    String[] params2 = input.split(",|，", 2);
                    if(params2.length != 2){
                        return null;
                    }
                    String beSpl1 = new String((String) paramMap.get(params2[0]));
                    String splIn1 = params2[1];
                    Queue<String> rets =new LinkedList<>(Arrays.asList(beSpl1.split(splIn1)));
                    if(ret == null) return null;
                    paramMap.put(ret,rets);
                    return ret;
                //3.对话
                case 3:
                    String response = aiUtils.chat3((String)paramMap.get(input));
                    if(response == null){
                        throw new AiException("ai异常");
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,response);
                    return ret;
                //4.拼接
                case 4:
                    Queue<String> strings = (Queue<String>) paramMap.get(input);
                    StringBuilder builder = new StringBuilder();
                    for(String str:strings){
                        builder.append(str);
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,builder.toString());
                    return ret;
                //5.全替换
                case 5:
                    String[] params5 = input.split(",|，",2);
                    if(params5.length != 2){
                        return null;
                    }
                    String beRep4 = new String((String) paramMap.get(params5[0]));
                    Queue<String> repIn4 = (Queue<String>) paramMap.get(params5[1]);
                    for(String str: repIn4){
                        beRep4 = beRep4.replaceFirst("\\[&]",str);
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,beRep4);
                    return ret;
                //6.行替换
                case 6:
                    String[] params3 = input.split(",|，",2);
                    if(params3.length != 2){
                        return null;
                    }
                    String beRep2 = new String((String) paramMap.get(params3[0]));
                    Queue<String> repIn2 = (Queue<String>) paramMap.get(params3[1]);
                    Queue<String> rets2 = new LinkedList<>();
                    for(String str: repIn2){
                        String cur = new String(beRep2);
                        rets2.offer(cur.replaceFirst("\\[&]",str));
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,rets2);
                    return ret;
                //7.列替换
                case 7:
                    String[] params4 = input.split(",|，",2);
                    if(params4.length != 2){
                        return null;
                    }
                    Queue<String> beRep3 = (Queue<String>)paramMap.get(params4[0]);
                    String repIn3 = (String) paramMap.get(params4[1]);
                    Queue<String> rets3 = new LinkedList<>();
                    for(String str: beRep3){
                        rets3.offer(str.replaceFirst("\\[&]",repIn3));
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,rets3);
                    return ret;
                //8.多分割
                case 8:
                    String[] params6 = input.split(",|，", 2);
                    if(params6.length != 2){
                        return null;
                    }
                    Queue<String> beSpl2 = new LinkedList<>((Queue<String>) paramMap.get(params6[0]));
                    String splIn2 = params6[1];
                    Queue<String[]> rets4 =new LinkedList<>();
                    for(String str:beSpl2){
                        rets4.offer(str.split(splIn2));
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,rets4);
                    return ret;
                //9.多对话
                case 9:
                    Queue<String> chats = (Queue<String>)paramMap.get(input);
                    Queue<String> rets5 = new LinkedList<>();
                    for(String str: chats){
                        String response2 = aiUtils.chat3(str);
                        if(response2 == null){
                            throw new AiException("ai异常");
                        }
                        rets5.offer(response2);
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,rets5);
                    return ret;
                //10.多拼接
                case 10:
                    Queue<String[]> joints = (Queue<String[]>) paramMap.get(input);
                    Queue<String> rets6 = new LinkedList<>();
                    for(String[] strings1:joints){
                        StringBuilder builder1 = new StringBuilder();
                        for(String str: strings1){
                            builder1.append(str);
                        }
                        rets6.offer(builder1.toString());
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,rets6);
                    return ret;
                //11.行全替换
                case 11:
                    String[] params7 = input.split(",|，",2);
                    if(params7.length != 2){
                        return null;
                    }
                    String beRep5 = new String((String) paramMap.get(params7[0]));
                    Queue<String[]> repIn5 = (Queue<String[]>) paramMap.get(params7[1]);
                    Queue<String> rets7 = new LinkedList<>();
                    for(String[] strings2: repIn5){
                        String cur = new String(beRep5);
                        for(String str: strings2 ){
                            cur = cur.replaceFirst("\\[&]",str);
                        }
                        rets7.offer(cur);
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,rets7);
                    return ret;
                //12.列全替换
                case 12:
                    String[] params8 = input.split(",|，",2);
                    if(params8.length != 2){
                        return null;
                    }
                    Queue<String> beRep6 = (Queue<String>)paramMap.get(params8[0]);
                    Queue<String> repIn6 = (Queue<String>) paramMap.get(params8[1]);
                    Queue<String> rets8 = new LinkedList<>();
                    for(String str: beRep6){
                        for(String str1:repIn6){
                            str = str.replaceFirst("\\[&]",str1);
                        }
                        rets8.offer(str);
                    }
                    if(ret == null) return null;
                    paramMap.put(ret,rets8);
                    return ret;
            }
        return null;
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 打印字符串
     * @Describe: TODO
     * @modifier: [public] [实例方法]
     **/
    @Override
    public String toString() {
        return "Step{" +
                "ret='" + ret + '\'' +
                ", func=" + func +
                ", param='" + input + '\'' +
                '}';
    }

}
